package com.yuexun.dhdoctor.module.commun.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.module.Contract.bean.GroupGroupBean;
import com.yuexun.dhdoctor.module.Contract.bean.GroupGroupData;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/8/9 0009.
 */

public class GroupExpandableCheckAdapter extends BaseExpandableListAdapter {
    private Context context;

    List<GroupGroupBean> groupGroupBeen = new ArrayList<>();
    List<GroupGroupData> selectValues =new ArrayList<>();

    //0：不能选择自己  1：能选择自己
    private int isSelectMe;
    //0:单选  1：多选
    private int type;
    Handler handler;
    boolean isDepart = false;

    public GroupExpandableCheckAdapter(Context context, List<GroupGroupBean> groupGroupBeen,
                                       ArrayList<GroupGroupData> selectValues, Handler handler, int isSelectMe, int type, boolean isDepart) {
        this.context = context;
        this.groupGroupBeen = groupGroupBeen;
        this.selectValues= selectValues;
        this.isSelectMe = isSelectMe;
        this.type = type;
        this.handler = handler;
        this.isDepart =isDepart;
    }

    public void setGroupGroupBeen(List<GroupGroupBean> groupGroupBeen) {
        this.groupGroupBeen = groupGroupBeen;
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        return null == groupGroupBeen ? 0 : groupGroupBeen.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return groupGroupBeen.get(i).getDatas().size();
    }

    @Override
    public Object getGroup(int i) {
        return groupGroupBeen.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return groupGroupBeen.get(i).getDatas().get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        GroupExpandableCheckAdapter.ViewGroupHolder holder;
        if (null == view) {
            view = LayoutInflater.from(context).inflate(R.layout.item_parent, null);
            holder = new GroupExpandableCheckAdapter.ViewGroupHolder();
            holder.groupName = (TextView) view.findViewById(R.id.item_group_name);
            view.setTag(holder);
        } else {
            holder = (GroupExpandableCheckAdapter.ViewGroupHolder) view.getTag();
        }

        holder.groupName.setText(groupGroupBeen.get(i).getGroup_name());
        return view;
    }

    @Override
    public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
        final GroupExpandableCheckAdapter.ViewChildHolder holder;
        if (null == view) {
            view = LayoutInflater.from(context).inflate(R.layout.item_group, null);
            holder = new GroupExpandableCheckAdapter.ViewChildHolder();
            holder.group_name = (TextView) view.findViewById(R.id.item_group_name);
            holder.group_count = (TextView) view.findViewById(R.id.item_group_count);
            holder.add_checkbox = (CheckBox) view.findViewById(R.id.add_checkbox);
            view.setTag(holder);
        } else {
            holder = (GroupExpandableCheckAdapter.ViewChildHolder) view.getTag();
        }
        holder.group_name.setText(groupGroupBeen.get(i).getDatas().get(i1).getTitle());
        holder.group_count.setText(groupGroupBeen.get(i).getDatas().get(i1).getToId().size() + "人");

        if(isDepart)
        {
            holder.add_checkbox.setVisibility(View.VISIBLE);
            holder.add_checkbox.setButtonDrawable(R.drawable.addmember_check);
            holder.add_checkbox.setEnabled(true);
        }
        else {
            holder.add_checkbox.setVisibility(View.GONE);
            holder.add_checkbox.setEnabled(false);
        }

        holder.add_checkbox.setChecked(false);

        for (GroupGroupData bean : selectValues) {
            if (bean.getGroupId().equals(groupGroupBeen.get(i).getDatas().get(i1).getGroupId())) {
                holder.add_checkbox.setChecked(true);
            }
        }


        holder.add_checkbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (type == 1) {
                    if (!((CheckBox) view).isChecked()) {
                        ArrayList<GroupGroupData> removeList = new ArrayList<>();
                        for (GroupGroupData bean : selectValues) {

                            if (bean.getGroupId().equals(groupGroupBeen.get(i).getDatas().get(i1).getGroupId())) {
                                removeList.add(bean);
                                break;
                            }
                        }
                        selectValues.removeAll(removeList);
//                        selectValues.remove(mDatas.get(i));
                        holder.add_checkbox.setChecked(false);
//                        notifyDataSetChanged();
                        Message msg = new Message();
                        msg.what = 10003;
                        handler.sendMessage(msg);
                    } else {
                        selectValues.add(groupGroupBeen.get(i).getDatas().get(i1));
                        holder.add_checkbox.setChecked(true);
                        notifyDataSetChanged();
                        Message msg = new Message();
                        msg.what = 10003;
                        handler.sendMessage(msg);
                    }
                } else if(type == 0){
                    if (!((CheckBox) view).isChecked()) {
                        selectValues.clear();
                        holder.add_checkbox.setChecked(false);
                        Message msg = new Message();
                        msg.what = 10003;
                        handler.sendMessage(msg);
                    } else {
                        selectValues.clear();
                        selectValues.add(groupGroupBeen.get(i).getDatas().get(i1));
                        holder.add_checkbox.setChecked(true);
                        notifyDataSetChanged();
                        Message msg = new Message();
                        msg.what = 10003;
                        handler.sendMessage(msg);
                    }
                }
            }
        });
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    class ViewGroupHolder {
        TextView groupName;
    }

    class ViewChildHolder {
        TextView group_name, group_count;
        CheckBox add_checkbox;
    }
}
